package com.sunyy.codegenerator.service.impl;


import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sunyy.codegenerator.common.MyException;
import com.sunyy.codegenerator.dto.templateGroup.SaveTemplateGroupDto;
import com.sunyy.codegenerator.entity.TemplateFileInfo;
import com.sunyy.codegenerator.entity.TemplateGroup;
import com.sunyy.codegenerator.mapper.TemplateGroupMapper;
import com.sunyy.codegenerator.service.CosService;
import com.sunyy.codegenerator.service.GroupVariableService;
import com.sunyy.codegenerator.service.TemplateFileInfoService;
import com.sunyy.codegenerator.service.TemplateGroupService;
import com.sunyy.codegenerator.util.SnowflakeUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 60382
 * @description 针对表【template_group(模板组合)】的数据库操作Service实现
 * @createDate 2025-07-19 10:58:14
 */
@Service
@Transactional
public class TemplateGroupServiceImpl extends ServiceImpl<TemplateGroupMapper, TemplateGroup>
        implements TemplateGroupService {
    private static final String bucketName = "code-generator";

    @Resource
    private TemplateFileInfoService templateFileInfoService;

    @Resource
    private CosService cosService;

    @Resource
    private GroupVariableService groupVariableService;

    @Override
    public void saveTemplateGroup(SaveTemplateGroupDto dto) {
        // 1. 通用参数鉴权
        if (dto == null) {
            throw new MyException("入参不能为空!!");
        }
        boolean isNewOne = dto.getPkid() == null || dto.getPkid() < 0;
        if (isNewOne) {
            // 新增
            if (StringUtils.isBlank(dto.getGroupName())) {
                throw new MyException("入参【groupName】不能为空!!");
            }
            if (StringUtils.isBlank(dto.getGroupDesc())) {
                throw new MyException("入参【groupDesc】不能为空!!");
            }
            dto.setPkid(SnowflakeUtil.getId());
        } else {
            // 修改
        }
        TemplateGroup entity = new TemplateGroup();
        BeanUtils.copyProperties(dto, entity);
        if (isNewOne) {
            save(entity);
        } else {
            updateById(entity);
        }
    }

    @Override
    public void deleteTemplateGroup(Long id) {
        if (id == null || id < 0) {
            throw new MyException("入参【id】非法!!");
        }
        TemplateGroup group = getById(id);
        if (group != null) {
            removeById(id);
        }
        // 删除文件
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("group_id", id);
        List<TemplateFileInfo> list = templateFileInfoService.list(wrapper);
        for (TemplateFileInfo fileInfo : list) {
            cosService.deleteFile(bucketName, fileInfo.getFileUrl());
        }
        templateFileInfoService.remove(wrapper);
        // 删除变量
        groupVariableService.remove(wrapper);
    }
}




